
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        *{
            margin:0;
            padding:0;
            list-style: none;
        }
        body{
        	height: 10000px;
        }
        .box{
            width:900px;
            margin:0 auto;
        }
        .box .layer{
            height:6000px;
            margin-top:50px;
            font-size:50px;
            text-align: center;
            line-height: 400px;
            color:white;
            background:orange;
        }
        .floor{
            position: fixed;
            bottom:5%;
            right:1%;
            padding:5px;
            cursor: pointer;
            border:1px solid #aaa;
            border-radius: 5px;
            color: #635c5c;
            transition: all 0.4s;
            transform: translate(0,-50%);
        }
        .floor li{
            padding:10px 5px;
            font-size: 14px;
        }
        .floor:hover{
        	color: #fff;
            border-color:#1576BA;
        	background-color: #00C4FF;
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="layer">盒子</div> 
    </div>
    <ul class="floor">
        <li>回到顶部啦~</li>
    </ul>
    <script>
        //获取楼层div
        var layer = document.getElementsByClassName('layer')[0];
        //获取楼层li
        var lis = document.getElementsByTagName('li')[0];
        lis.onclick=function(){
            //调用运动函数
            var res = running(0);
        }
        var xh=null;
        function running(num){ //目标值   500    0
            xh=setInterval(function(){
                //目标值与当前值之间的距离
                var scrollTop = document.body.scrollTop || document.documentElement.scrollTop;
                var jl=(num-scrollTop)/10;
                if(document.documentElement.scrollTop){
                    document.documentElement.scrollTop=scrollTop+jl;
                }else{
                    document.body.scrollTop=scrollTop+jl;
                }
                //判断当前文档滚动高度是否与目标一致
                if(scrollTop==0){
                    //停止循环
                    clearInterval(xh);
                }
            },30);
        }
    </script>
</body>
</html>